include config.mak
include i264e/sub.mak
include ijpege/sub.mak
include ijpegd/sub.mak
include hwicodec/sub.mak
include icommon/sub.mak

all: libicodec.a

INCLUDES = ${INCLUDE_COMMON} -I$(I265E)/include -I$(I265E)/comkit/include \
		   -I$(I264E)/include -I$(IJPEGE)/include -I$(IJPEGD)/include -I$(HWICODEC)/include \
		   -I$(HWICODEC)/src/protocol/include -I../../include -I../../../../include/api/cn \
		   -I$(ICOMMON)/include
CFLAGS = ${CFLAGS_COMMON} $(INCLUDES) -mmxu2 -flax-vector-conversions -Wall -DICODEC_HELIX_TEST
CPPFLAGS = ${CFLAGS_COMMON} $(INCLUDES) -Wno-invalid-offsetof
LDFLAGS = -lpthread -lm

C_SUBLIB = $(I264E) $(IJPEGE) $(IJPEGD) $(HWICODEC) $(ICOMMON)
#CPP_SUBLIB = $(I265E)

C_OBJS = $(foreach sublib, $(C_SUBLIB), $(foreach objs, $(OBJS-$(sublib)), $(addprefix $(sublib)/, $(objs))))
CPP_OBJS = $(foreach sublib, $(CPP_SUBLIB), $(foreach objs, $(OBJS-$(sublib)), $(addprefix $(sublib)/, $(objs))))
GCC_VERSION_DIR=$(shell $(CC) -v 2>&1 | sed -n '/gcc version/p' | awk -F " " '{print $$3}')

VPURCOJB += ../../../../external/${GCC_VERSION_DIR}/vpu/obj/glibc/jzm_enc_api_nofpic_t21.o

$(C_OBJS):%.o:%.c
	$(CC) -c $(CFLAGS) -o $@ $<

$(CPP_OBJS):%.o:%.cpp
	$(CPP) -c $(CPPFLAGS) -o $@ $<

libicodec.a: $(C_OBJS) $(CPP_OBJS) $(VPURCOJB)
	$(LD) -EL -o $@ -r -Bstatic $^

#libi265e.a:
#	$(MAKE) libi265e.a -C $(I265E)

libi264e.a:
	$(MAKE) libi264e.a -C $(I264E)

libijpege.a:
	$(MAKE) libijpege.a -C $(IJPEGE)

libijpegd.a:
	$(MAKE) libijpegd.a -C $(IJPEGD)

libhwicodec.a:
	$(MAKE) -C $(HWICODEC)

libicommon.a:
	$(MAKE) -C $(ICOMMON)

install:
	mkdir -p out
	#cp $(I265E)/include/i265e.h out
	cp $(I264E)/include/i264e.h out
	cp $(IJPEGE)/include/ijpege.h out
	cp $(IJPEGD)/include/ijpegd.h out
	cp $(ICOMMON)/include/icommon.h out
	cp libicodec.a out

uninstall:
	rm -rf out

.PHONY: clean distclean
clean:
	-rm -f $(C_OBJS) $(CPP_OBJS)

distclean: clean
	#$(MAKE) distclean -C $(I265E)
	$(MAKE) distclean -C $(I264E)
	$(MAKE) distclean -C $(IJPEGE)
	$(MAKE) distclean -C $(IJPEGD)
	rm -rf out
	-rm -f libicodec.a
